<?php

class ColorCalc {
	protected $aColors;
	
	public function __construct() {
		$this->aColors = array();
		$this->aColors ['STARTCOLOR']	= array('RED' => 255, 'GREEN' => 0,   'BLUE' => 0 );
		$this->aColors ['MIDCOLOR']	= array('RED' => 238, 'GREEN' => 136, 'BLUE' => 17);
		$this->aColors ['ENDCOLOR']	= array('RED' => 0,   'GREEN' => 170, 'BLUE' => 0 );
	}

	public function getRGBCalc($pct) {
		$r1 = 0;
		$r2 = 0;
		$g1 = 0;
		$g2 = 0;
		$b1 = 0;
		$b2 = 0;
		$calcRed = 0;
		$calcGreen = 0;
		$calcBlue = 0;
	 
		# Sanitize the percent is '0 <= pct <= 100'
		$this->cleanPctRange($pct);
		//$pct = $pct < 0 ? 0 : $pct;
		//$pct = $pct > 100 ? 100 : $pct;
		
		# Going from start color to mid color
		if( $pct <= 50 ) {
			$pct *= 2;
			$r1 = $this->aColors['STARTCOLOR']['RED'];		$r2 = $this->aColors['MIDCOLOR']['RED'];
			$g1 = $this->aColors['STARTCOLOR']['GREEN'];		$g2 = $this->aColors['MIDCOLOR']['GREEN'];
			$b1 = $this->aColors['STARTCOLOR']['BLUE'];		$b2 = $this->aColors['MIDCOLOR']['BLUE'];
		} 
	
		# Going from Midcolor to endcolor
		else {
			$pct -= (100-$pct);
			$r1 = $this->aColors['MIDCOLOR']['RED'];		$r2 = $this->aColors['ENDCOLOR']['RED'];
			$g1 = $this->aColors['MIDCOLOR']['GREEN'];		$g2 = $this->aColors['ENDCOLOR']['GREEN'];
			$b1 = $this->aColors['MIDCOLOR']['BLUE'];		$b2 = $this->aColors['ENDCOLOR']['BLUE'];
		}
		
		# Crunch the numbers
		if( $r1 < $r2 )	{ $calcRed	= floor($r1 + (($r2-$r1)*($pct/100))); }
		else		{ $calcRed	= floor($r1 - (($r1-$r2)*($pct/100))); }

		if( $g1 < $g2 )	{ $calcGreen	= floor($g1 + (($g2-$g1)*($pct/100))); }
		else		{ $calcGreen	= floor($g1 - (($g1-$g2)*($pct/100))); }

		if( $b1 < $b2 ) { $calcBlue	= floor($b1 + (($b2-$b1)*($pct/100))); }
		else		{ $calcBlue	= floor($b1 - (($b1-$b2)*($pct/100))); }
		
		return "$calcRed, $calcGreen, $calcBlue";
	}
	
	protected function cleanPctRange(&$pct) {
		$pct = $pct < 0 ? 0 : $pct;
		$pct = $pct > 100 ? 100 : $pct;	
	}

	public function setStartColor($r,$g,$b) {
		$this->aColors['STARTCOLOR']['RED']	= $r;
		$this->aColors['STARTCOLOR']['GREEN']	= $g;
		$this->aColors['STARTCOLOR']['BLUE']	= $b;
	}

	public function setMidColor($r,$g,$b) {
		$this->aColors['MIDCOLOR']['RED']	= $r;
		$this->aColors['MIDCOLOR']['GREEN']	= $g;
		$this->aColors['MIDCOLOR']['BLUE']	= $b;
	}

	public function setEndColor($r,$g,$b) {
		$this->aColors['ENDCOLOR']['RED']	= $r;
		$this->aColors['ENDCOLOR']['GREEN']	= $g;
		$this->aColors['ENDCOLOR']['BLUE']	= $b;
	}
	
	public function dump() {
		return	"START:{$this->aColors['STARTCOLOR']['RED']},{$this->aColors['STARTCOLOR']['GREEN']},{$this->aColors['STARTCOLOR']['BLUE']}\n".
			"MID:{$this->aColors['MIDCOLOR']['RED']},{$this->aColors['MIDCOLOR']['GREEN']},{$this->aColors['MIDCOLOR']['BLUE']}\n".
			"END:{$this->aColors['ENDCOLOR']['RED']},{$this->aColors['ENDCOLOR']['GREEN']},{$this->aColors['ENDCOLOR']['BLUE']}\n";
	}

	public function dumpHTML() {
		for($i=0; $i<=100; $i++) {
			$returnVal .= "<span style=\"background-color:rgb(".$this->getRGBCalc($i).");\">&nbsp;</span>";
		}
		return	"<span style=\"border:solid black 1px;\">$returnVal</span>";
	}

}

